/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package program.gui;

import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JCheckBox;
import org.hsqldb.Database;
import org.omg.PortableServer.POA;
import program.beans.Category;

/**
 *
 * Responsible: Patrick, Bernhard
 */
public class SelectCategroiesDlg extends javax.swing.JDialog {

    
    /**
     * Creates new form SelectCategroiesDlg
     */
    
    private JCheckBox catBoxes[];
    private boolean ok;
    private ArrayList<Category> selectedCats = new ArrayList<>();
    private ArrayList<Category> allCategories;
    public SelectCategroiesDlg(java.awt.Frame parent, boolean modal, ArrayList<Category> allCategories, ArrayList<Category> selected) {
        
        super(parent, modal);
        this.allCategories = allCategories;
        initComponents();
        pnCat.setLayout(new GridLayout(5, (allCategories.size()/5)));
        catBoxes = new JCheckBox[allCategories.size()];
        
        for(int i = 0; i < allCategories.size();i++)
        {
            Category c = allCategories.get(i);
            JCheckBox jcb = new JCheckBox(c.getDescription());
            if(selected.contains(c))
                jcb.setSelected(true);
            catBoxes[i] = jcb;
            pnCat.add(jcb);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        pnCat = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Kategorien auswählen:");

        jPanel1.setLayout(new java.awt.GridBagLayout());

        jButton1.setText("OK ");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                onOK(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        jPanel1.add(jButton1, gridBagConstraints);

        jButton2.setText("Abbrechen");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                onCancel(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        jPanel1.add(jButton2, gridBagConstraints);

        jButton3.setText("Alle auswählen");
        jButton3.setToolTipText("");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                onSelectAll(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        jPanel1.add(jButton3, gridBagConstraints);

        jButton4.setText("Auswahl aufheben");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                onDeselect(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        jPanel1.add(jButton4, gridBagConstraints);

        getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_END);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Kategorien"));
        jPanel2.setLayout(new java.awt.BorderLayout());

        javax.swing.GroupLayout pnCatLayout = new javax.swing.GroupLayout(pnCat);
        pnCat.setLayout(pnCatLayout);
        pnCatLayout.setHorizontalGroup(
            pnCatLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 386, Short.MAX_VALUE)
        );
        pnCatLayout.setVerticalGroup(
            pnCatLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 232, Short.MAX_VALUE)
        );

        jScrollPane1.setViewportView(pnCat);

        jPanel2.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void onSelectAll(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_onSelectAll
        for(JCheckBox jcb : catBoxes)
        {
            jcb.setSelected(true);
        }
    }//GEN-LAST:event_onSelectAll

    private void onDeselect(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_onDeselect
        for(JCheckBox jcb : catBoxes)
        {
            jcb.setSelected(false);
        }
    }//GEN-LAST:event_onDeselect

    private void onOK(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_onOK
        for(int i = 0; i < catBoxes.length; i++)
        {
            JCheckBox jcb = catBoxes[i];
            if(jcb.isSelected())
                selectedCats.add(allCategories.get(i));
        }
        ok = true;
        dispose();
    }//GEN-LAST:event_onOK

    private void onCancel(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_onCancel
        ok = false;
        dispose();
    }//GEN-LAST:event_onCancel

    public boolean isOk() {
        return ok;
    }

    public ArrayList<Category> getSelectedCats() {
        return selectedCats;
    }

    /**
     * @param args the command line arguments
     */
    
    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel pnCat;
    // End of variables declaration//GEN-END:variables
}
